package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.StaffEntity;
import com.tfjybj.itoo.teach.model.StaffModel;
import com.tfjybj.itoo.teach.provider.service.StaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * StaffController
 * staff表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"staff表接口"})
@RequestMapping(value = "/staff")
@RestController
public class StaffController {

    @Resource
    private StaffService staffService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StaffModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @Transactional
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StaffModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        StaffEntity staffEntity = new StaffEntity();
        BeanUtils.copyProperties(model, staffEntity);
        staffService.save(staffEntity);
        //添加角色信息
        String institutionId = model.getInstitutionId();
        boolean addAuto = staffService.addToAuth(staffEntity,institutionId);
        if(addAuto = false){
            return  ItooResult.build(ItooResult.FAIL, "添加失败");
        }else{
            return ItooResult.build(ItooResult.SUCCESS, "添加成功");
        }
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        staffService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        staffService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StaffModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改staff")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StaffModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        StaffEntity staffEntity = new StaffEntity();
        BeanUtils.copyProperties(model, staffEntity);
        staffService.updateById(staffEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Staff
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        StaffEntity staffEntity = staffService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffEntity);
    }

    /**
     * 分页查询所有Staff
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Staff")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffEntity> staffs = staffService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffs);
    }

    /**
     * 根据教师姓名模糊查询staff
     *
     * @param name     教师姓名
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的staff
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据教师姓名模糊查询staff", notes = "分页根据教师姓名模糊查询staff")
    @GetMapping(value = "queryStaffByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryStaffByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                           @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                           @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffModel> staffList = staffService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据教师编码模糊查询staff
     *
     * @param code 教师编码
     * @return 模糊查询的staff
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据教师编码查询staff", notes = "根据教师编码查询staff")
    @GetMapping(value = "queryByCode/{code}")
    public ItooResult queryByCode(@ApiParam(name = "code", value = "教师编码", required = true) @PathVariable String code) {

        List<StaffModel> staffModelList = staffService.queryByCode(code);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffModelList);
    }

    /**
     * 分页查询所有教师信息（初始化页面）
     *
     * @return staffModel
     * @author 张家豪
     * @since 2018.11.22 17:07
     */
    @ApiOperation(value = "分页查询所有教师信息（初始化页面）", notes = "分页查询所有教师信息（初始化页面）")
    @GetMapping(value = "queryAllStaff/{pageNo}/{pageSize}")
    public ItooResult queryAllStaff(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                    @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffModel> staffModelList = staffService.queryAllStaff(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffModelList);
    }

    /**
     * 高级搜索-根据职工号或者机构id查询职工信息
     *
     * @param code 教职工号， academy 机构名称
     * @return staffModel
     * @author 张家豪
     * @since 2018年11月23日08:37:47
     */
    @ApiOperation(value = "高级搜索-根据职工号或者机构id查询职工信息")
    @GetMapping(value = "queryStaffByCodeAndAcademy/{pageNo}/{pageSize}")
    public ItooResult queryStaffByCodeAndAcademy(@RequestParam(required = false, defaultValue = " ") String code, @RequestParam(required = false, defaultValue = " ") String academy, @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo, @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffModel> staffList = staffService.queryStaffByCodeAndAcademy(pageNo, pageSize, code, academy);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", staffList);
    }

    /**
     * 导出
     *
     * @param staffCode 教职工编码
     * @param staffName 教师姓名
     * @param academy   学院id
     * @return staffModel
     * @author 张家豪
     * @since 2018年11月24日14:34:11
     */
    @ApiOperation(value = "导出")
    @GetMapping(value = "exportFile/exportStaffInfo")
    public ItooResult exportStaffInfo(HttpServletResponse response, @RequestParam(required = false, defaultValue = " ") String staffName, @RequestParam(required = false, defaultValue = " ") String staffCode, @RequestParam(required = false, defaultValue = " ") String academy) {
        return staffService.exportStaffInfo(response, staffName, staffCode, academy);
    }

    /**
     * 根据多个机构查询staff
     *
     * @param academys 机构id集合
     * @return staffModel
     * @author 张家豪
     * @since 2018.11.24 20:43
     */
    @ApiOperation(value = "根据多个机构查询staff")
    @PostMapping(value = {"/queryByAcademys/{pageNo}/{pageSize}"})
    public ItooResult queryByAcademys(@RequestBody List<String> academys, @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo, @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffModel> staffs = staffService.queryByAcademys(academys, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffs);
    }

    /**
     * 批量导入staff
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 张家豪
     * @since 1.0.0 2018年11月25日10:21:29
     */
    @ApiOperation(value = "批量导入staff")
    @PostMapping(value = {"/importStaff"})
    public ItooResult importStaff(@RequestParam(value = "file") MultipartFile multipartFile,
                                  HttpServletRequest request, HttpServletResponse response) {
        return staffService.importStaff(multipartFile, request, response);
    }

    /**
     * 下载教师模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 张家豪
     * @since 2.0.0 2018年11月25日14:57:40
     */
    @ApiOperation(value = "下载教师模板，批量导入使用", notes = "下载教师模板，批量导入使用")
    @GetMapping(value = {"/downLoadTemplate"})
    public ItooResult downTemplate(HttpServletResponse response) {
        if (staffService.downTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载培养计划模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载培养计划模板失败");
        }
    }

    /**
     * 查询所有教师信息-接口
     *
     * @return staffEntity
     * @author 张家豪
     * @since 2018.11.27 15:56
     */
    @ApiOperation(value = "查询某学院所有教师信息-接口")
    @GetMapping(value = "queryAllStaffInfo/{institutionId}")
    public ItooResult queryAllStaffInfo(@ApiParam(value = "机构id", required = true) @PathVariable String institutionId) {
        List<StaffEntity> staffList = staffService.queryAllStaffInfo(institutionId);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", staffList);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 张家豪
     * @since 2018.11.16 19:25
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (staffService.downErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }


}

